package com.cloudxingkong.dianyinke.Student.Ui;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Adapter.IntegralDetailsAdapter;
import com.cloudxingkong.dianyinke.Student.bean.IntegraldetailsEntity;
import com.cloudxingkong.dianyinke.Uri.studentUri.StudentUri;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的积分Activity
 */
public class IntegralDetails extends BaseActivity {

    private List<IntegraldetailsEntity.ObjBean.ExpendlistBean.ListBean>list;
    private ListView listView;
    private IntegralDetailsAdapter adapter;
    private SharedPreferences sharedPreferences;
    private ProgressDialog progressDialog;
    private TextView tv1, tv2;
    private int position;

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setContentView(R.layout.activity_integral_details);
        init();
        dataLoad();
    }

    private void dataLoad() {
        progressDialog = ProgressDialog.show(IntegralDetails.this, null, "正在加载...", true, false);
        final String sessionid = CookieHelper.getSessionID(sharedPreferences);
        final String upLoadUrl = ThisUri.SERVER_YUMING + StudentUri.STUDNETPOINTSQRYDETAIL;
        RequestParams params = new RequestParams(upLoadUrl);
        params.addHeader("cookie", sessionid);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("text", "积分详情：" + result);
                if (result != null) {
                    Message message = handler.obtainMessage();
                    message.what = 1;
                    message.obj = result;
                    handler.sendMessage(message);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                progressDialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                String state = (String) msg.obj;
                if (state != null) {
                        Gson gson = new Gson();
                        IntegraldetailsEntity entity = gson.fromJson(state, IntegraldetailsEntity.class);
                        List<IntegraldetailsEntity.ObjBean.ExpendlistBean.ListBean> listBeans = entity.getObj().getExpendlist().getList();
                        tv1.setText(entity.getObj().getCurrpoints()+"");
                        tv2.setText(entity.getObj().getHistpoints() + "");
                        IntegralDetails.this.list.addAll(listBeans);
                        adapter.notifyDataSetChanged();
                        progressDialog.dismiss();
                        String  dangqianjifen =entity.getObj().getCurrpoints()+"";
                        String  zongjifen =entity.getObj().getHistpoints()+"";
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("dangqianjifen",dangqianjifen);
                        editor.putString("zongjifen",zongjifen);
                        editor.commit();
//                    } catch (Exception e) {
//                        Log.e("test", "Details " + e.getMessage());
//                        e.printStackTrace();
//
//                    }
                }
            }
        }

    };

    private void init() {
        sharedPreferences = IntegralDetails.this.getSharedPreferences("data_dyk", 0);
        listView = (ListView) findViewById(R.id.lv_integralDetails);
        tv1=(TextView) findViewById(R.id.zongjifeng);
        tv2 = ((TextView) findViewById(R.id.dangqianjifen));
        list = new ArrayList<>();
        adapter = new IntegralDetailsAdapter(list,this);
        listView.setAdapter(adapter);
    }

    //返回按钮
    public void back(View view) {
        finish();
    }
    public void guize(View view) {
        Toast.makeText(IntegralDetails.this, "现木有规则！", Toast.LENGTH_SHORT).show();
    }
}

